﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Media;
using Android.Graphics;
using Android.Locations;
using AndroidText = Android.Text;
using Android.Content.Res;
using Android.Support.V4.App;
using Android.Net;

namespace MWC.NetcampCheckin
{
    [BroadcastReceiver]
    public class NotificationUpdateManager : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            var prefs = context.GetSharedPreferences("NetcampNotification", 0);
            long lastUpdate = prefs.GetLong("LastUpdate", 0);
            //domyslna wartosc odswierzania ustawiona na dzien
            long refreshValue = 8640000000;
            ConnectivityManager cm = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);

            if (cm.ActiveNetworkInfo == null || !cm.ActiveNetworkInfo.IsConnectedOrConnecting)
            {
                //nie ma interneru wiec nic nie rob
            }
            else
            {

                try
                {


                    NetcampWebProxy.WebProxy proxy = new NetcampWebProxy.WebProxy();
                    proxy.Proxy = null;
                    NetcampWebProxy.CheckAllNotification result = proxy.GetCurrentNotifications(lastUpdate);
                    refreshValue = result.repeatTime;
                    var prefsEditor = prefs.Edit();
                    prefsEditor.PutLong("LastUpdate", DateTime.Now.Ticks);
                    prefsEditor.Commit();

                    int i = 0;
                    foreach (var current in result.notifications)
                    {
                        //ustaw numery notyfikacji aby sie nie powtarzaly
                        int notificationNumber = 1000 + i;
                        i++;

                        NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context)
                        .SetSmallIcon(Resource.Drawable.Icon);

                        nBuilder.SetContentText(current.message)
                            .SetContentTitle(current.title)
                            .SetAutoCancel(true);
                        if (current.uriSite != string.Empty)
                        {
                            Intent notificationIntent = new Intent(Intent.ActionView, global::Android.Net.Uri.Parse(current.uriSite));
                            PendingIntent contentIntent = PendingIntent.GetActivity(context, 0, notificationIntent, 0);
                            nBuilder.SetContentIntent(contentIntent);
                        }

                        NotificationManager notificationManager = (NotificationManager)context.GetSystemService(Context.NotificationService);
                        notificationManager.Notify(notificationNumber, nBuilder.Build());

                    }

                }
                catch (InvalidCastException e)
                {

                }

            }

            AlarmManager alarmManager = (AlarmManager)context.GetSystemService(Context.AlarmService);
            Intent service = new Intent(context, typeof(NotificationUpdateManager));
            PendingIntent pendingIntent = PendingIntent.GetBroadcast(context, 0, service, PendingIntentFlags.CancelCurrent);
            long utcMilis = (long)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalMilliseconds;
            alarmManager.Set(AlarmType.RtcWakeup, utcMilis + refreshValue, pendingIntent);
            //Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();

            
        }
    }
}